package LeetCode.LeetCode2;

import java.util.Arrays;

/**
 * 合并两个有序数组
 * @author lumin
 * @date 2022/03/22 21:56
 **/
public class Num88 {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        if(m == 0){
            for (int i = 0; i < n; i++) {
                nums1[m + i] = nums2[i];
            }
            return;
        }
        int i = 0;
        int j = 0;
        while(j < n){
            while(i < m){
                if(nums1[i] < nums2[j]){
                    i++;
                    continue;
                }else if(nums1[i] == nums2[j]) {
                    for (int k = m + n - 1; k > i + 1; k--) {
                        nums1[k] = nums1[k - 1];
                    }
                    nums1[i + 1] = nums2[j];
                    i++;
                    j++;
                }
            }
            nums1[i] = nums2[j];
            i++;
            j++;
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {1,0};
        int[] nums2 = {2};
        merge(nums1, 0, nums2, nums2.length);
        System.out.println(Arrays.toString(nums1));
    }
}